package com.game.eggscatcher.GameObjects;

import java.util.Random;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;
import com.game.eggscatcher.GameHelpers.AssetLoader;
import com.game.eggscatcher.GameWorld.GameWorld;

public class GiaCam {
	private Array<Fallable> listTrung = new Array<Fallable>();
	private Ro ro;
	private GameWorld gameWorld;
	private Pool<Fallable> trungPool = new Pool<Fallable>() {
	
		@Override
		protected Fallable newObject() {
			// TODO Auto-generated method stub
			return new Fallable();
		}
		
	};
	private Random random;
	int trungSpeed = 30;
	private Vector2 position;
	private int velocityX;
	public final int WIDTH = 15;
	public final int HEIGHT = 15;
	private int periodTrung = 5;
	private int periodShit = 7;
	private float countTrung = 0, countShit = 0;
	private boolean isTrung = true;
	public GiaCam(float x, float y, int v, int level, GameWorld world){
		gameWorld = world;
		position = new Vector2(x, y);
		velocityX = v;
		trungSpeed += 25*level;
		random = new Random();
		this.ro = gameWorld.getRo();
		countTrung = random.nextInt(4);
	}
	
	public void update(float delta) {
		position.x += velocityX * delta; 
		if (position.x > 136 - WIDTH || position.x < 0) velocityX = -velocityX;
		
		countTrung += delta;
		countShit += delta;
		if (countTrung >= periodTrung) { 
//			trung.reset(position.x + WIDTH/2, position.y + HEIGHT/2);
			AssetLoader.laid.play();
			Fallable trung = trungPool.obtain();
			if (countShit >= periodShit) {
				isTrung = false;
				countShit = 0;
				periodShit = random.nextInt(30)+20;
			} else {
				isTrung = true;
			}
			trung.init(position.x, position.y + HEIGHT, trungSpeed, isTrung);
			listTrung.add(trung);
			countTrung = random.nextInt(3);
		}
		
		updateListTrung(delta);
	}
	private void updateListTrung(float delta) {
		for (int i = listTrung.size; --i >= 0;) {
			Fallable trung = listTrung.get(i);
			if (trung.alive) {
				trung.update(delta);
				if (trung.collides(ro)) {
					System.out.println("Collides");
					trung.alive = false;
					if (trung.isTrung) { // neu la trung thi tang diem
						AssetLoader.scored.play();
						ro.addScore(1);
						if (ro.getScore() % 20 == 0) {
							AssetLoader.level_up.play();
							gameWorld.gainLevel();
//							ro.levelUp(); 
						}
					} else { // neu la shit thi giam live (3 live cho shit)
						AssetLoader.shit_catched.play();
						ro.decShitLives();
						if (ro.getShitLives() <= 0) {
							AssetLoader.game_over.play();
							gameWorld.gameOver();
//							ro.reset();
						}
					}
				}
			} else {
				if (trung.isTrung && trung.isOutScreen()) { // neu la trung roi ngoai man hin thi giam live (3 live cho trung)
					ro.decTrungLives();
					if (ro.getTrungLives() <= 0) {
						AssetLoader.game_over.play();
						gameWorld.gameOver();
//						ro.reset();
					}
				}
				listTrung.removeIndex(i);
				trungPool.free(trung);
			}
		}
	}
	public int getCountTrung() {
		return listTrung.size;
	}
	public Fallable getTrung(int index) {
		return listTrung.get(index);
	}
	public float getX() {
		return position.x;
	}
	public float getY() {
		return position.y;
	}
	
}
